package com.ego.dubbo.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.dubbo.service.TbItemParamDubboService;
import com.ego.mapper.TbItemParamItemMapper;
import com.ego.mapper.TbItemParamMapper;
import com.ego.pojo.TbItemParam;
import com.ego.pojo.TbItemParamExample;
import com.ego.pojo.TbItemParamItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class TbItemParamDubboServiceImpl implements TbItemParamDubboService {

    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Override
    public PageInfo<TbItemParam> selectTbItemParamByPage(int page, int size) {

        PageHelper.startPage(page, size);

        List<TbItemParam> tbItemParamItems = tbItemParamMapper.selectByExampleWithBLOBs(null);

        PageInfo<TbItemParam> pageInfo = new PageInfo<>(tbItemParamItems);

        return pageInfo;
    }

    @Override
    public TbItemParam selectByItemCatId(long itemCatId) {


        TbItemParamExample tbItemParamExample = new TbItemParamExample();
        tbItemParamExample.createCriteria().andItemCatIdEqualTo(itemCatId);

        List<TbItemParam> tbItemParams = tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);

        return tbItemParams.size() == 1 ? tbItemParams.get(0) : null;
    }

    @Override
    public int insert(TbItemParam tbItemParam) {

        return tbItemParamMapper.insert(tbItemParam);

    }

    @Override
    public int deleteByIds(List<Long>  ids) throws DaoException {

        try {
            TbItemParamExample tbItemParamExample = new TbItemParamExample();
            tbItemParamExample.createCriteria().andIdIn(ids);

            int rows = tbItemParamMapper.deleteByExample(tbItemParamExample);

            if (rows ==ids.size()){
                return 1;
            }else{
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new DaoException("批量删除商品规格参数失败！");
        }

    }
}
